#include "maingame.h"
#include "ui_maingame.h"


maingame::maingame(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::maingame)
    , scene(new QGraphicsScene(this))
    , player(new character())  // 创建角色
{
    ui->setupUi(this);

    // 初始化设置
    setFocusPolicy(Qt::StrongFocus);  // 允许接收键盘事件
    initScene();
    // 添加角色到场景
    scene->addItem(player);
    player->setPos(400, 300);  // 初始位置

    // 设置视图
    ui->gameview->setScene(scene);
    ui->gameview->setRenderHint(QPainter::Antialiasing);
    ui->gameview->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);


    connect(ui->backButton,&QPushButton::clicked,[=](){
        //因为此类里面没有widget的对象，所以想要返回只能用父类的函数调控父类
        emit this->back();
    });

    this->battle1=new battle;//实例化注游戏窗口
    connect(ui->fightButton,&QPushButton::clicked,[=](){

        this->hide();
        this->battle1->show();
    });
    connect(this->battle1,&battle::back,[=](){
        this->battle1->hide();
        this->show();
    });





}


void maingame::initScene()
{
    const int sceneWidth = 1200;
    const int sceneHeight = 800;//整个

    scene->setSceneRect(0, 0, sceneWidth, sceneHeight);
    scene->setBackgroundBrush(Qt::darkCyan);

    // 加载地图图像
    QPixmap prePixmap(":/find/textmap.png");
    QPixmap mapPixmap = prePixmap.scaled(
        1200, 800,        // 强制缩放到目标尺寸
        Qt::IgnoreAspectRatio,  // 忽略宽高比
        Qt::SmoothTransformation
        );


    // 检查图像加载是否成功
    if (mapPixmap.isNull()) {
        // 加载失败时创建一个占位图案
        mapPixmap = QPixmap(sceneWidth, sceneHeight);
        QPainter painter(&mapPixmap);
        painter.fillRect(0, 0, sceneWidth, sceneHeight, Qt::darkBlue);

        // 绘制网格线以表示地图
        painter.setPen(Qt::lightGray);
        for (int x = 0; x < sceneWidth; x += 50)
            painter.drawLine(x, 0, x, sceneHeight);
        for (int y = 0; y < sceneHeight; y += 50)
            painter.drawLine(0, y, sceneWidth, y);

        painter.end();
    }

    // 创建地图项并添加到场景
    mapItem = new QGraphicsPixmapItem(mapPixmap);
    scene->addItem(mapItem);

    // 确保地图居中显示
    mapItem->setPos(
        (sceneWidth - mapPixmap.width()) / 2,
        (sceneHeight - mapPixmap.height()) / 2
        );
}





void maingame::keyPressEvent(QKeyEvent* event)
{
    // 将按键事件传递给角色
    player->keyPressEvent(event);

    // 可选：处理返回信号
    if(event->key() == Qt::Key_Escape) {
        emit back();
    }

    QWidget::keyPressEvent(event);
}



maingame::~maingame()
{
    delete ui;
}


// 具体技能实现
Skill *p1=new Skill("水枪", 40, Element::Water);
Skill *p2=new Skill("喷射火焰", 90, Element::Fire);

void maingame::on_fightButton_clicked() {
    // 创建宝可梦（示例数据，实际应从游戏状态获取）
    //皮卡丘
    Pokemon* playerPokemon1 = new Pokemon("皮卡丘", 80, 45, 35, Element::Rock, ":/images/pikachu.png");
    playerPokemon1->addSkill(p1);  // 假设已有Thunderbolt技能
    playerPokemon1->addSkill(p2);  // 假设已有QuickAttack技能
    //杰尼龟
    Pokemon* enemyPokemon = new Pokemon("杰尼龟", 90, 40, 40, Element::Water, ":/images/squirtle.png");
    enemyPokemon->addSkill(p1);
    enemyPokemon->addSkill(p2);




    // 设置战斗界面的宝可梦
    battle1->setPlayerPokemon(playerPokemon1);
    battle1->setEnemyPokemon(enemyPokemon);

    // 开始战斗
    battle1->startBattle();

    // 显示战斗界面（模态对话框）
    battle1->setWindowModality(Qt::ApplicationModal);
    battle1->show();
}
